package com.ych.modules.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ych.modules.base.dbEntity.TSysChargeRuleEntity;
import com.ych.modules.base.mapper.TSysChargeRuleMapper;
import com.ych.modules.base.service.ITSysChargeRuleService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/18
 * Time: 14:52
 * Description:
 */
@Service("ITSysChargeRuleService")
public class TSysChargeRuleServiceImpl extends ServiceImpl<TSysChargeRuleMapper, TSysChargeRuleEntity> implements ITSysChargeRuleService {
    @Override
    public List<TSysChargeRuleEntity> listChargeRule() {
        Wrapper<TSysChargeRuleEntity> wrapper = new EntityWrapper<TSysChargeRuleEntity>()
                .where("1=1");
        List<TSysChargeRuleEntity> list = this.selectList(wrapper);
        return list;
    }

    @Override
    public void modifyChargeRule(TSysChargeRuleEntity entity) {
        TSysChargeRuleEntity ruleEntity = this.selectById(entity.getId());
        if (null == ruleEntity) {
            throw new RuntimeException("目标规则不存在");
        }
        if (null == entity.getGoldCoinAmount()) {
            throw new RuntimeException("必传参数为空");
        }
        if (BigDecimal.ZERO.compareTo(entity.getGoldCoinAmount()) >= 0) {
            throw new RuntimeException("不能设置为0金币以下");
        }
        TSysChargeRuleEntity tempEntity = new TSysChargeRuleEntity();
        tempEntity.setId(ruleEntity.getId());
        tempEntity.setGoldCoinAmount(ruleEntity.getGoldCoinAmount());
        this.insert(tempEntity);
    }

    @Override
    public TSysChargeRuleEntity getChargeRule(String chargeBusinessType) {
        Wrapper<TSysChargeRuleEntity> wrapper = new EntityWrapper<TSysChargeRuleEntity>()
                .where("1=1")
                .eq("charge_business_type", chargeBusinessType);
        TSysChargeRuleEntity entity = this.selectOne(wrapper);
        return entity;
    }
}
